<template>
  <!-- 图片创建页面 -->
  <div id="addPicturePage">  
    <h2 style="margin-bottom: 16px">  
  {{ route.query?.id ? '修改图片' : '创建图片' }}  
</h2>
    <a-typography-paragraph v-if="spaceId" type="secondary">
      保存至空间：<a :href="`/space/${spaceId}`" target="_blank">{{ spaceId }}</a>
    </a-typography-paragraph>


  <!-- 调用图片上传组件 -->
  <!-- 选择上传方式 -->  
<a-tabs v-model:activeKey="uploadType"  
  >>  
  <a-tab-pane key="file" tab="文件上传">  
    <PictureUpload :picture="picture" :onSuccess="onSuccess" />  
  </a-tab-pane>  
  <a-tab-pane key="url" tab="URL 上传" force-render>  
    <UrlPictureUpload :picture="picture" :onSuccess="onSuccess" />  
  </a-tab-pane>  
</a-tabs>
    <div v-if="picture" class="edit-bar">
      <a-button :icon="h(EditOutlined)" @click="doEditPicture">编辑图片</a-button>
      <ImageCropper
          ref="imageCropperRef"
          :imageUrl="picture?.url"
          :picture="picture"
          :spaceId="spaceId"
          :onSuccess="onSuccess"
        />
    </div>
  <a-form
      v-if="picture"
      name="pictureForm"
      layout="vertical"
      :model="pictureForm"
      @finish="handleSubmit"
    >
      <a-form-item name="name" label="名称">
        <a-input v-model:value="pictureForm.name" placeholder="请输入名称" allow-clear />
      </a-form-item>
      <a-form-item name="introduction" label="简介">
        <a-textarea
          v-model:value="pictureForm.introduction"
          placeholder="请输入简介"
          :auto-size="{ minRows: 2, maxRows: 5 }"
          allow-clear
        />
      </a-form-item>
      <a-form-item name="category" label="分类">
        <a-auto-complete
          v-model:value="pictureForm.category"
          placeholder="请输入分类"
          :options="categoryOptions"
          allow-clear
        />
      </a-form-item>
      <a-form-item name="tags" label="标签">
        <a-select
          v-model:value="pictureForm.tags"
          mode="tags"
          placeholder="请输入标签"
          :options="tagOptions"
          allow-clear
        />
      </a-form-item>
      <a-form-item>
        <a-button type="primary" html-type="submit" style="width: 100%">创建</a-button>
      </a-form-item>
    </a-form>
</div>
</template>

<script setup lang="ts">

import { computed, h, onMounted, reactive, ref, watchEffect } from 'vue'
import PictureUpload from "../components/PictureUpload.vue"
import UrlPictureUpload from '../components/UrlPictureUpload.vue'
import { EditOutlined, FullscreenOutlined } from '@ant-design/icons-vue'
import { message } from 'ant-design-vue'
import { useRouter,useRoute } from 'vue-router'
import {editPictureUsingPost,listPictureTagCategoryUsingGet,getPictureByIdUsingGet,getPictureVoByIdUsingGet} from '../api/pictureController'
import ImageCropper from '../components/ImageCropper.vue'


const router=useRouter()
const route = useRoute()  


const uploadType = ref<'file' | 'url'>('file')
const picture = ref<API.PictureVO>()  
const onSuccess = (newPicture: API.PictureVO) => {  
  picture.value = newPicture  
  pictureForm.name = newPicture.name  
}

const pictureForm = reactive<API.PictureEditRequest>({})
// 分类和标签选项数据
const categoryOptions=ref<string[]>([])
const tagOptions=ref<string[]>([])

// 空间 id
const spaceId = computed(() => {
  return route.query?.spaceId
})



// 表单提交处理函数
const handleSubmit=async (values:any)=>{
  // 根据上传图片得到图片id，然后调用修改接口将编辑的信息附带着上传上去
    const pictureId=picture.value.id
    if(!pictureId){
        return
    }
    const res = await editPictureUsingPost({
        id: pictureId,
        spaceId: spaceId.value,
        ...values,
      })

    if(res.data.code===0&&res.data.data){
      message.success('创建成功')
      // 跳转到图片详情页
      router.push({
        path: `/picture/${pictureId}`,
        // meta: { id:pictureId }
      })
    }
    else{
      message.error('创建失败'+res.data.message)
    } 
}

// 向后端获取标签和分类选项
const getTagCategoryOptions=async()=>{
  const res= await listPictureTagCategoryUsingGet()
  if(res.data.code===0 && res.data.data){
    // 转换成下拉选项组件接受的格式
    tagOptions.value=(res.data.data.tagList ??[]).map((data:string)=>{
      return {
        value:data,
        label:data,
      }
    })
    categoryOptions.value = (res.data.data.categoryList ?? []).map((data: string) => {
      return {
        value: data,
        label: data,
      }
    })
  }
  else {
    message.error('加载选项失败,'+res.data.message)
  }
}

onMounted(()=>{
  getTagCategoryOptions()
})


onMounted(() => {
  getOldPicture()
})
// 获取老数据  
const getOldPicture = async () => {  
  // 获取数据  
  const id = route.query?.id 
  if (id) {  
    const res = await getPictureVoByIdUsingGet({  
      id: id,  
    })  
    if (res.data.code === 0 && res.data.data) {  
      
      const data = res.data.data  
      picture.value = data  
      pictureForm.name = data.name  
      pictureForm.introduction = data.introduction  
      pictureForm.category = data.category  
      pictureForm.tags = data.tags  
    }  
  }  
} 

// 图片编辑弹窗引用
const imageCropperRef = ref()

// 编辑图片
const doEditPicture = () => {
  if (imageCropperRef.value) {
    imageCropperRef.value.openModal()
  }
}

// 编辑成功事件
const onCropSuccess = (newPicture: API.PictureVO) => {
  picture.value = newPicture
}





</script>

<style scoped>
#addPicturePage {  
  max-width: 720px;  
  margin: 0 auto;  
}

#addPicturePage .edit-bar {
  text-align: center;
  margin: 16px 0;
}


</style>

